#include "ObjectPool.h"
#include "ConcurrentAlloc.h"


void Alloc1()
{
	for (size_t i = 0; i < 5; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
	}
}

void Alloc2()
{
	for (size_t i = 0; i < 5; ++i)
	{
		void* ptr = ConcurrentAlloc(8);
	}
}

void TLSTest()
{
	std::thread t1(Alloc1);
	t1.join();

	std::thread t2(Alloc2);
	t2.join();
}

void TestConcurrentAlloc1()
{
	void* p1 = ConcurrentAlloc(7);
	void* p2 = ConcurrentAlloc(8);
	void* p3 = ConcurrentAlloc(6);
	void* p4 = ConcurrentAlloc(8);
	void* p5 = ConcurrentAlloc(5);
	void* p6 = ConcurrentAlloc(7);
}

void TestConcurrentAlloc2()
{
	void* ptrArr[1024];
	for (size_t i = 0; i < 1024; i++)
	{
		ptrArr[i] = ConcurrentAlloc(8);
	}

	for (size_t i = 0; i < 1024; i++)
	{
		cout << ptrArr[i] << endl;
	}

	void* ptr = ConcurrentAlloc(8);
	cout << ptr << endl;

}

void TestConcurrentFree1()
{
	void* p1 = ConcurrentAlloc(7);	//1		1 + 2 + 3 + 4 = 10 10 - 7 = 3
	void* p2 = ConcurrentAlloc(8);	//2
	void* p3 = ConcurrentAlloc(6);	
	void* p4 = ConcurrentAlloc(8);	//3
	void* p5 = ConcurrentAlloc(5);	
	void* p6 = ConcurrentAlloc(7);	
	void* p7 = ConcurrentAlloc(8);	//4

	ConcurrentFree(p1);
	ConcurrentFree(p2);
	ConcurrentFree(p3);
	ConcurrentFree(p4);
	ConcurrentFree(p5);
	ConcurrentFree(p6);
	ConcurrentFree(p7);

}

void MutilAlloc1()
{
	void* ptrArr[7];
	for (size_t i = 0; i < 7; i++)
	{
		ptrArr[i] = ConcurrentAlloc(7);
	}

	for (size_t i = 0; i < 7; i++)
	{
		//ConcurrentFree(ptrArr[i], 8);
		ConcurrentFree(ptrArr[i]);
	}
}

void MutilAlloc2()
{
	void* ptrArr[7];
	for (size_t i = 0; i < 7; i++)
	{
		ptrArr[i] = ConcurrentAlloc(13);
	}

	for (size_t i = 0; i < 7; i++)
	{
		//ConcurrentFree(ptrArr[i], 16);
		ConcurrentFree(ptrArr[i]);
	}
}

void TestConcurrentFree2()
{
	std::thread t1(MutilAlloc1);
	std::thread t2(MutilAlloc2);
	t1.join();
	t2.join();
}

//void BigAlloc()
//{
//	void* p1 = ConcurrentAlloc(257 * 1024);
//	ConcurrentFree(p1);
//
//	void* p2 = ConcurrentAlloc(129 * 8 * 1024);
//	ConcurrentFree(p2);
//
//}
//
//int main()
//{
//	//TestObjectPool();
//	//TLSTest();
//	//TestConcurrentlloc2();
//	//TestConcurrentFree1();
//	//TestConcurrentFree2();
//
//	BigAlloc();
//
//	return 0;
//}